package com.xiaojf.xlog.appender.rdb.configure;

import com.xiaojf.xlog.appender.AccessLogAppender;
import com.xiaojf.xlog.appender.rdb.AccessLogRdbAppender;
import com.xiaojf.xlog.appender.rdb.entity.AccessLog;
import com.xiaojf.xlog.appender.rdb.repository.AccessLogRepository;
import com.xiaojf.xlog.appender.rdb.service.AccessLogService;
import com.xiaojf.xlog.appender.rdb.service.impl.AccessLogServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * rdb 保存访问日志配置
 *
 * @author Ade.Xiao 2021/3/12 13:42
 */
@ConditionalOnMissingBean(AccessLogAppender.class)
@ConditionalOnProperty(prefix = "xlog.access", name = "appender-type", havingValue = "rdb")
@Configuration
@EnableJpaRepositories(basePackageClasses = {AccessLogRepository.class})
@EntityScan(basePackageClasses = {AccessLog.class})
public class AccessLogRdbAppenderConfigure {
    @Resource
    private AccessLogRepository accessLogRepository;

    @PostConstruct
    public void postConstruct() {
        System.out.println("使用rdb记录访问日志...");
    }

    @Bean
    public AccessLogService accessLogService() {
        return new AccessLogServiceImpl(accessLogRepository);
    }

    @Bean
    @Primary
    public AccessLogAppender accessLogAppender(AccessLogService accessLogService) {
        return new AccessLogRdbAppender(accessLogService);
    }
}
